package com.khaled.protclass.model.feature.composition;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.khaled.protclass.model.base.BaseFeature;
import com.khaled.protclass.model.base.IProtein;
import com.khaled.protclass.model.bean.weka.Attribute;
import com.khaled.protclass.model.bean.weka.WekaDataTypes;
import com.khaled.protclass.model.util.UtilAminoAcids;

/**
 * Dipeptide composition is the number of occurrence of a dipeptide divided by
 * the number of possible dipeptides.
 * 
 * @author hassankh
 */
public class DipeptideComposition extends BaseFeature
{
	/** Logger. */
	private static Logger logger = Logger.getLogger(DipeptideComposition.class);

	// CONSTANTS related to the feature
	private static String NAME = "DPC";

	@Override
	public String getName()
	{
		return NAME;
	}

	@Override
	public ArrayList<Attribute> getAttributes()
	{
		logger.info("getAttributes starts.");

		ArrayList<Attribute> attributes = new ArrayList<Attribute>();

		for (String dep : UtilAminoAcids.getInstance().getAminoDipeptides())
		{
			attributes.add(new Attribute(dep, WekaDataTypes.NUMERIC));
		}

		logger.info("Number of attributes:" + attributes.size());
		logger.info("getAttributes ends.");
		return attributes;
	}

	@Override
	public String getFeatureVector(IProtein protein)
	{
		logger.info("getFeatureVector starts.");

		StringBuffer featureVector = new StringBuffer();
		String sequence = protein.getProteinSequence();

		logger.info("Protein ID: " + protein.getProteinID());
		logger.info("Protein Classification:" + protein.getClassification());
		logger.info("Protein Sequence:" + protein.getProteinSequence());

		for (String dep : UtilAminoAcids.getInstance().getAminoDipeptides())
		{
			int freq = 0; // number of occurrence of a dipeptide
			int count = 0; // number of possible dipeptides

			for (int i = 0; i < sequence.length() - 1; i++)
			{
				String prot_dep = Character.toString(sequence.charAt(i)) + Character.toString(sequence.charAt(i + 1));

				if (dep.equals(prot_dep))
				{
					freq++;
				}
				count++;
			}
			double avgFreq = (double) (freq / (double) count);

			logger.info("Frequency of Depeptide (" + dep + ") = " + freq);
			logger.info("Avg Frequency of Depeptide (" + dep + ") = " + avgFreq);

			featureVector.append(avgFreq).append(",");
		}
		logger.info("Feature Vector:" + featureVector);

		return featureVector.toString();
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}
}
